package com.ycy.chuwenuniapp.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Config {

    //是否开启swagger，正式环境一般是需要关闭的
    @Value("${swagger.enable:true}")
    private boolean enableSwagger = true;


    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否开启 (true 开启  false隐藏。生产环境建议隐藏)
                .enable(enableSwagger)
                .select()
                //扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //.apis(RequestHandlerSelectors.basePackage("cn.xstk.zhdg.gongdi"))
                //指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger2接口文档API")
                .description("本文档描述了接口定义规范")
                .version("1.0")
                .contact(new Contact("ycy", "http://htwlgs.cn", "854462730@qq.com"))
                .build();
              //.termsOfServiceUrl("http://localhost:8080/")//端口号
    }
}

//swagger文档中在 afterScript设置请求头
// var code=ke.response.data.code;
// if(code==200){
//     //判断,如果服务端响应code是8200才执行操作
//     //获取token
//     var token=ke.response.data.data.token;
//     //1、如何参数是Header，则设置当前逻辑分组下的全局Header
//     ke.global.setHeader("token",token);
//     //2、如果全局参数是query类型,则设置当前逻辑分组下的全局Parameter,开发者自行选择
//     ke.global.setParameter("token",token);
// }